package com.dd.ent.car.common.player;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.dd.ent.car.R;


/**
 * Author:LiuJianSheng
 * Date:2016/6/2
 */
public class MediaPlayerErrorView extends FrameLayout implements MediaPlayerStateChangeListener{
    private ThunderMediaPlayer mPlayer;
    public MediaPlayerErrorView(Context context) {
        super(context);
        initView(context);
    }

    public MediaPlayerErrorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MediaPlayerErrorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
    
    private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.media_player_error_view,this,true);
        View retryBtn = findViewById(R.id.retry_btn);
        retryBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mPlayer != null){
                    mPlayer.rePlay();
                }
            }
        });
        setVisibility(GONE);
    }

    public void setMediaPlayer(ThunderMediaPlayer player) {
        this.mPlayer = player;
    }

    @Override
    public void onStateChange(MediaPlayerState oldState, MediaPlayerState newState) {
        if(newState == MediaPlayerState.ERROR){
            setVisibility(VISIBLE);
        }else{
            setVisibility(GONE);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        return true;
    }
}
